package com.ruoyi.community.comment.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.core.collection.CollectionUtil;
import com.ruoyi.comment.domain.PostPlayComment;
import com.ruoyi.comment.vo.PostPlayCommentListVo;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.community.comment.vo.CommunityCommentListVo;
import com.ruoyi.community.domain.CommunityPost;
import com.ruoyi.community.service.ICommunityPostService;
import com.ruoyi.play.domain.PostPlay;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.community.comment.domain.CommunityComment;
import com.ruoyi.community.comment.service.ICommunityCommentService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * communitycommentController
 *
 * @author ruoyi
 * @date 2025-05-22
 */
@RestController
@RequestMapping("/community/comment")
public class CommunityCommentController extends BaseController {
    @Autowired
    private ICommunityCommentService communityCommentService;
    @Autowired
    private ISysUserService sysUserService;
    @Autowired
    private ICommunityPostService communityPostService;

    /**
     * 查询communitycomment列表
     */
    @GetMapping("/list")
    public TableDataInfo list(CommunityComment communityComment) {
        if (communityComment.getCommunityId() == null) {
            throw new ServiceException("帖子id不能为空");
        }
        List<CommunityCommentListVo> res = new ArrayList<>();
        LoginUser loginUser = getLoginUser();
        Long userId = loginUser.getUser().getUserId();
        // 如果是组队发起者,则不添加用户id筛选
        List<CommunityComment> list = communityCommentService.selectCommunityCommentList(communityComment);
        if (CollectionUtil.isNotEmpty(list)) {
            List<Long> userIdList = list.stream().map(CommunityComment::getUserId).collect(Collectors.toList());
            List<SysUser> sysUserList = sysUserService.selectUserByIds(userIdList);
            Map<Long, SysUser> userMap = sysUserList.stream().collect(Collectors.toMap(SysUser::getUserId, Function.identity()));
            list.forEach(comment -> {
                CommunityCommentListVo commentListVo = new CommunityCommentListVo();
                SysUser user = userMap.get(comment.getUserId());
                if (user != null) {
                    commentListVo.setUserId(user.getUserId());
                    commentListVo.setNickName(user.getNickName());
                    commentListVo.setAvatar(user.getAvatar());
                    boolean equals = user.getUserId().equals(userId);
                    commentListVo.setMe(equals);
                }
                commentListVo.setCommunityComment(comment);
                res.add(commentListVo);
            });
        }
        return getDataTable(res);
    }

    /**
     * 导出communitycomment列表
     */
    @Log(title = "communitycomment", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CommunityComment communityComment) {
        List<CommunityComment> list = communityCommentService.selectCommunityCommentList(communityComment);
        ExcelUtil<CommunityComment> util = new ExcelUtil<CommunityComment>(CommunityComment.class);
        util.exportExcel(response, list, "communitycomment数据");
    }

    /**
     * 获取communitycomment详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(communityCommentService.selectCommunityCommentById(id));
    }

    /**
     * 新增communitycomment
     */
    @Log(title = "communitycomment", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CommunityComment communityComment) {
        String content = communityComment.getContent();
        Long communityId = communityComment.getCommunityId();
        if (content == null) {
            throw new ServiceException("评论内容不能为空");
        }
        if (communityId == null) {
            throw new ServiceException("所评论的帖子id不能为空");
        }
        communityComment.setUserId(SecurityUtils.getLoginUser().getUser().getUserId());
        communityComment.setCommentTime(new Date());
        CommunityPost communityPost = communityPostService.selectCommunityPostById(communityId);
        if (communityPost == null) {
            throw new ServiceException("所评论的帖子不存在");
        }
        int res = communityCommentService.insertCommunityComment(communityComment);
        if (res > 0) {
            communityPost.setCommentNumber(communityPost.getCommentNumber() + 1L);
            communityPostService.updateCommunityPost(communityPost);
        }
        return toAjax(res);
    }

    /**
     * 修改communitycomment
     */
    @Log(title = "communitycomment", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CommunityComment communityComment) {
        return toAjax(communityCommentService.updateCommunityComment(communityComment));
    }

    /**
     * 删除communitycomment
     */
    @Log(title = "communitycomment", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(communityCommentService.deleteCommunityCommentByIds(ids));
    }
}
